--
-- Author: ping.wang
-- Date: 2018-07-10 16:40:13
--
-- AuctionStorageView
--
local NewListAdapterTool = require("app.widget.NewListAdapterTool")
local AuctionStorageItemRender = import(".renders.AuctionStorageItemRender")
local AuctionStorageIconListRender = import(".renders.AuctionStorageIconListRender")
local table_insert = table.insert

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.AuctionStoragePanel

function ClassRef:init()
    self._ui.Button_close:addClickEventListener(function()
        executeFunc(self._onCloseView)
    end)

    self._moneyIcon = self._ui.Panel_money:findChild("icon")
    self._moneyNum = self._ui.Panel_money:findChild("num")


    self._allItems = {}
    self._equips = {}
    self._consumables = {}
    self._others = {}

    self._itemsAry = {self._allItems, self._equips, self._consumables, self._others }

    self._titleText = self._ui.Panel_tlt:findChild("tlt")
    self._title1 = self._ui.Panel_r:findChild("Panel_tlt_1") --普通商品售卖的时候显示
    self._title2 = self._ui.Panel_r:findChild("Panel_tlt_2") --竞价商品售卖的时候显示
    self._leftScrollPanel = self._ui.Panel:findChild("Panel_r/ScrollView")
    self._rightScrollPanel = self._ui.Panel:findChild("Panel_bag/ScrollView")

    local params = {
        list = self._leftScrollPanel,
        subPanelClass = AuctionStorageItemRender
    }
    self._productListnode = NewListAdapterTool.new( params )
    self._productListnode:setSubPanelCB( handler( self, self._productListRenderOpCb ) )


    self._storageNum = self._ui.Panel_r:findChild("Panel_bom/text")
    local totalSellPlace = GD:getSystemPar( "auctionShelvesLimite" )
    self._storageNum:setString(string.format(L("auction_storageLimite"),0,totalSellPlace))

    --显示背包中可以售卖的商品
    local params2 = {
        list = self._rightScrollPanel,
        subPanelClass = AuctionStorageIconListRender
    }
    self._bagItemListNode = NewListAdapterTool.new( params2 )
    self._bagItemListNode:setSubPanelCB( handler( self, self._productListRenderOpCb ) )
    self._curRightPanelIndex = 0
    for i = 1 , 4 do
        local bagBtn = self._ui["Panel/Panel_bag/Panel_top/Button_"..i]
        bagBtn:setTouchEnabled( true )
        bagBtn:setBright( true )
        bagBtn:addClickEventListener(function( sender )

            self:showItemsByIndex(i)
        end)
    end

    self:showItemsByIndex(1)
end

function ClassRef:showItemsByIndex(idx)
    if self._curRightPanelIndex ~= idx then
        self._curRightPanelIndex = idx
        for i = 1 , 4 do
            self._ui["Panel/Panel_bag/Panel_top/Button_"..i]:setBright( true )
            self._ui["Panel/Panel_bag/Panel_top/name_"..i]:setOpacity( 255 * 0.45 ) -- 45%透明度
        end
        self._ui["Panel/Panel_bag/Panel_top/Button_"..self._curRightPanelIndex ]:setBright( false )
	    self._ui["Panel/Panel_bag/Panel_top/name_"..self._curRightPanelIndex ]:setOpacity( 255 )

        --根据当前选中的tab刷新数据
        self:setSelectedBagItems(idx)
    end
end

function ClassRef:refreshTitle(isNormal)
    if isNormal then
        self._title1:setVisible(true)
        self._title2:setVisible(false)
        self._titleText:setString(L("auction_saleTitle2"))
    else
        self._title1:setVisible(false)
        self._title2:setVisible(true)
        self._titleText:setString(L("auction_saleTitle1"))
    end
end

function ClassRef:initMoneyPanel(tabId)
    if tabId == Enums.AuctionProductType.normal then
        local coinId = tonumber(GD:getSystemPar( "auctionNormalGoodsLootType",99))
        local coinConfig = GD:getCoinConfig(coinId)
        local iconPath = string.format("%s%s.png",Res.coinIconPath,coinConfig.icon)
        self._moneyIcon:loadTexture(iconPath)
        self._moneyNum:setString(tostring(UD:getDaibiValue(coinId)))
    else
        local coinId = 99
        local publisher = app:getInnerPublisher()
        if publisher == Enums.Publisher.tencent then
            coinId = tonumber(GD:getSystemPar( "auctionBiddingGoodsLootType_tencent",99))
        else
            coinId = tonumber(GD:getSystemPar( "auctionBiddingGoodsLootType",115))
        end
        local coinConfig = GD:getCoinConfig(coinId)
        local iconPath = string.format("%s%s.png",Res.coinIconPath,coinConfig.icon)
        self._moneyIcon:loadTexture(iconPath)
        self._moneyNum:setString(tostring(UD:getDaibiValue(coinId)))
    end
end

function ClassRef:refresh(data)
    if data then
        self._data = data

        self:refreshTitle(data.isNormal)

        self._leftScrollPanel:setVisible(true)
        self._rightScrollPanel:setVisible(true)

        self._allItems = {}
        self._equips = {}
        self._consumables = {}
        self._others = {}
        self._itemsAry = {self._allItems, self._equips, self._consumables, self._others }

        local tabId = app:getInst("AuctionController"):getCurrentSelectTabIdx()
        --此处要根据显示普通还是竞价显示不同的货币
        self:initMoneyPanel(tabId)
        -- table.sort(data.bagItemData, function(a, b)
        --     local actype_a = a:getAcType()
        --     local actype_b = b:getAcType()
        --     if actype_a ~= actype_b then
        --         return actype_a == tabId
        --     end
        --     return false
        -- end)

        for k,item in pairs(data.bagItemData) do
            if item.itemBagType == Enums.ItemBagType.equip then
                self._equips[#self._equips + 1] = item
            elseif item.itemBagType == Enums.ItemBagType.item then
                if item:isConsumables() then
                    self._consumables[#self._consumables + 1] = item
                else
                    self._others[#self._others + 1] = item
                end
            end
            self._allItems[#self._allItems + 1] = item
        end

        self._productListnode:refreshList( data.saleGoods)
        self:setSelectedBagItems(self._curRightPanelIndex)
        self._leftScrollPanel :setVisible(true)
        self._rightScrollPanel:setVisible(true)
        local totalSellPlace = GD:getSystemPar( "auctionShelvesLimite" )
        self._storageNum:setString(string.format(L("auction_storageLimite"),#data.saleGoods,totalSellPlace))
    end
end

function ClassRef:setSelectedBagItems(idx)
    if idx >= 1 and idx <= 5 then
        self._bagItemListNode:refreshList(self:_getBagItemSellData(self._itemsAry[idx]))
    end
end


--背包中的道具显示使用了NewListAdpaterTool 数据格式需要重组
function ClassRef:_getBagItemSellData(data)
    local bagitemData = {}
    local n = 0
    for i = 1,#data do
        if (i - 1)%5 == 0 then
            n = n + 1
            table_insert(bagitemData,{})
        end
        table_insert(bagitemData[n],data[i])
    end
    --dump(bagitemData)
    return bagitemData
end

function ClassRef:onOperate(cb)
    self._onOperate = cb
end

function ClassRef:_productListRenderOpCb( op, data )
    -- print( " ------------>>>>> _runeListRenderOpCb ", op, runeRender:getName() )
    executeFunc(self._onOperate, op, data)
end

function ClassRef:hide()
    self:setVisible(false)
    self._leftScrollPanel:setVisible(false)
    self._rightScrollPanel:setVisible(false)
    self._title1:setVisible(false)
    self._title2:setVisible(false)
end

--添加了顶部切换

return ClassRef
